<template>
  <div>
    <!-- 表单 -->
    <el-form ref="addForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="departmentNameFocus" />
        <el-tree v-if="showTree" :data="list" :props="{label:'name'}" @node-click="selectNode" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
      <el-form-item>
        <el-button @click="hCancel">取消</el-button>
        <el-button type="primary" @click="hSubmit">确定</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
import { addEmployee } from '@/api/employees'
// 设计模式把聘用的值改为正式非正式
import EmployeeEnum from '@/constant/employees'
// 转树状结构
import { tranListToTreeData } from '@/utils/index'
// 引入api方法
import { getDepartments } from '../../api/departmenrs'
export default {
  data() {
    return {
      list: [],
      showTree: false,
      hireType: EmployeeEnum.hireType,
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  async created() {
    const res = await getDepartments()
    console.log(res)
    this.list = tranListToTreeData(res.data.data.depts)
  },
  methods: {
    // 点击取消隐藏弹出层
    hCancel() {
      this.$emit('close')
    },
    // 兜底验证
    hSubmit() {
      this.$refs.addForm.validate(valid => {
        //   判断里面是否有值
        if (valid) {
          console.log('添加')
          this.doAdd()
        }
      })
    },
    // 点击输入框让树状团显示
    departmentNameFocus() {
      this.showTree = true
    },
    // 点击树状结构的子节点赋给到input里去
    selectNode(nodeData) {
      if (nodeData.children.length) {
        return
      }
      this.formData.departmentName = nodeData.name
      this.showTree = false
    },
    // 新增员工接口判断是否添加成功
    async doAdd() {
      try {
        const res = await addEmployee(this.formData)
        console.log(res)
        this.$emit('Refres')
        this.$emit('close')
        this.$message.success('添加成功')
      } catch (error) {
        this.$message.error(error)
      }
    },
    remove() {
      console.log('清空')
      // 1. 把表单的数据项清空
      this.formData.username = '' // 用户名
      this.formData.mobile = '' // 手机号
      this.formData.formOfEmployment = '' // 聘用形式
      this.formData.workNumber = '' // 工号
      this.formData.departmentName = '' // 部门
      this.formData.timeOfEntry = '' // 入职时间
      this.formData.correctionTime = ''// 转正时间
      // 2.把验证结果清空
      this.$nextTick(() => {
        this.$refs.addForm.clearValidate()
      })
    }
  }
}
</script>

<style>

</style>
